package com.xinzhitong.www.service.impl;


import com.xinzhitong.www.enumeration.CollectionType;
import com.xinzhitong.www.exception.BusyException;
import com.xinzhitong.www.exception.collection.CollectionExistException;
import com.xinzhitong.www.exception.collection.CollectionNotExistException;
import com.xinzhitong.www.mapper.NewsMapper;
import com.xinzhitong.www.mapper.ShareMapper;
import com.xinzhitong.www.pojo.User;
import com.xinzhitong.www.service.ShareService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 分享服务实现
 *
 * @author 吴展鹏
 */
@Service
@Transactional
public class ShareServiceImpl implements ShareService {

    @Resource
    private ShareMapper shareMapper;


    //void addAShare(Integer shareId, CollectionType shareType )
    @Override
    public void addAShare(Integer shareId, CollectionType shareType) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        String username = user.getUsername();
        String idField = "post_id";

        //表名 = "user_share_" + 收藏类型
        String table = "user_share_post_or_consign";
        if (shareType.toString().toLowerCase().equals("course")) {
            table = "user_share_train_class_time";
            idField = "train_class_time_id";
        }
        if (shareMapper.insertShare(table, idField, username, shareId) == 0)
            throw new BusyException();
    }
}
